package com.ruoyi.shop.task;

/**
 * @Author: JinChen Zhu
 * @CreateTime: 2024-06-26  14:54
 */

import com.github.javafaker.Faker;
import com.ruoyi.shop.modules.domain.Order;
import com.ruoyi.shop.modules.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component("DataGenerator")
@Slf4j
public class OrderDataGenerator {

    private final Faker faker = new Faker();
    @Autowired
    private OrderMapper orderMapper;


    public void generateRandomOrder() {
        new Thread(() -> {
            // 生成随机订单
            Order order = randomOrder();
            try {
                // 模拟异步写入数据库
                TimeUnit.MILLISECONDS.sleep(faker.number().numberBetween(1, 10000));
                orderMapper.insertOrder(order);
            } catch (InterruptedException e) {
                log.error("生成随机订单失败", e);
            }
        }, "Thread-" + faker.number().randomNumber()).start();
    }

    public Order randomOrder() {
        Order order = new Order();
        order.setMemberId(faker.number().numberBetween(1L, 1000L));
        order.setProductId(faker.number().numberBetween(1L, 100L));
        order.setCouponId(faker.number().randomNumber()); // 使用Optional处理可能的null值
        order.setOrderSn(faker.bothify("????-####-###"));
        order.setMemberUsername(faker.internet().emailAddress());
        order.setTotalAmount(BigDecimal.valueOf(faker.number().randomDouble(2, 0, 10000)));
        order.setPayAmount(BigDecimal.valueOf(faker.number().randomDouble(2, 0, 5000)));
        order.setFreightAmount(BigDecimal.valueOf(faker.number().randomDouble(2, 0, 1000)));
        order.setPromotionAmount(BigDecimal.valueOf(faker.number().randomDouble(2, 0, 500)));
        order.setIntegrationAmount(BigDecimal.valueOf(faker.number().randomDouble(2, 0, 100)));
        order.setCouponAmount(BigDecimal.valueOf(faker.number().randomDouble(2, 0, 100)));
        order.setDiscountAmount(BigDecimal.valueOf(faker.number().randomDouble(2, 0, 50)));
        order.setPayType((long) faker.number().numberBetween(0, 2));
        order.setSourceType((long) faker.number().numberBetween(0, 1));
        order.setStatus((long) faker.number().numberBetween(0, 5));
        order.setOrderType((long) faker.number().numberBetween(0, 1));
        order.setDeliveryCompany(faker.company().name());
        order.setDeliverySn(faker.bothify("D###"));
        order.setAutoConfirmDay((long) faker.number().numberBetween(1, 30));
        order.setIntegration((long) faker.number().numberBetween(10, 1000));
        order.setGrowth((long) faker.number().numberBetween(10, 100));
        order.setBillType((long) faker.number().numberBetween(0, 2));
        order.setBillHeader(faker.name().fullName());
        order.setBillReceiverPhone(faker.phoneNumber().cellPhone());
        order.setBillReceiverEmail(faker.internet().emailAddress());
        order.setReceiverName(faker.name().fullName());
        order.setReceiverPhone(faker.phoneNumber().cellPhone());
        order.setReceiverPostCode(faker.address().zipCode());
        order.setReceiverProvince(faker.address().state());
        order.setReceiverCity(faker.address().city());
        order.setReceiverRegion(faker.address().cityName());
        order.setReceiverDetailAddress(faker.address().fullAddress());
        order.setConfirmStatus((long) faker.number().numberBetween(0, 1));
        order.setDeleteStatus(0L);
        order.setUseIntegration((long) faker.number().numberBetween(1, 100));

        // 设置时间字段
//        order.setCreateTime(new Date(System.currentTimeMillis()));//数据库自己设置
        order.setPaymentTime(Date.from(faker.date().past(50, TimeUnit.DAYS).toInstant()));
        order.setDeliveryTime(Date.from(faker.date().past(30, TimeUnit.DAYS).toInstant()));
        order.setReceiveTime(Date.from(faker.date().past(15, TimeUnit.DAYS).toInstant()));
        order.setCommentTime(Date.from(faker.date().past(10, TimeUnit.DAYS).toInstant()));
        order.setModifyTime(Date.from(faker.date().past(5, TimeUnit.DAYS).toInstant()));

        return order;
    }
}